﻿using DevExpress.XtraEditors;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BW.Mes.WinForms.Process
{
    public partial class AddProcessForm : DevExpress.XtraEditors.XtraForm
    {
        public AddProcessForm()
        {
            InitializeComponent();
        }
        

        private void labelControl1_Click(object sender, EventArgs e)
        {

        }

        private void labelControl4_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 添加按钮方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void simpleButton1_Click(object sender, EventArgs e)
        {
            ///将文本框中的内容赋给实体
            var model = new AddProcessModel
            {
                processName = textEdit1.Text,
                processExplain = textEdit2.Text,
                processDesc = textEdit3.Text,
                processHead = textEdit4.Text,
                processDepartment = comboBoxEdit1.Text,
                processworkshop = comboBoxEdit2.Text,
                processMaterials = textEdit7.Text,
                processadEnable = radioGroup1.SelectedIndex == 1 // 假设1为“是”
            };
            ///把表单数据以 POST 方式发送到后端接口
            using (var client = new HttpClient())
            {
                /// 设置请求地址（路由后缀）
                client.BaseAddress = new Uri("https://localhost:7222");
                ///// 创建 JSON 数据(转数据类型)
                var json = JsonConvert.SerializeObject(model);
                /// 创建请求内容(指定编码为 UTF-8，内容类型为 application/json)
                var content = new StringContent(json, Encoding.UTF8, "application/json");
                /// 发送 POST 请求
                var response = await client.PostAsync("/api/ProcessModel/AddProcessModel", content);
                ///判断是否成功
                if (response.IsSuccessStatusCode)
                {
                    XtraMessageBox.Show("添加成功！");
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    XtraMessageBox.Show("添加失败：" + response.ReasonPhrase);
                }
            }
        }

        private void checkEdit1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void comboBoxEdit2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void AddProcessForm_Load(object sender, EventArgs e)
        {

        }


        /// <summary>
        /// 需要添加实体
        /// </summary>
        public class AddProcessModel
        {
            public string processName { get; set; }
            public string processExplain { get; set; }
            public string processDesc { get; set; }
            public string processHead { get; set; }
            public string processDepartment { get; set; }
            public string processworkshop { get; set; }
            public string processMaterials { get; set; }
            public bool processadEnable { get; set; }
        }
    }
}